package com.infore.water.controller;

import com.infore.water.service.DataService;
import com.infore.water.service.RealDataService;
import com.infore.water.util.ListObject;
import com.infore.water.util.ResponseUtil;
import com.infore.water.util.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;

/**
 * @author:wk
 */
@RestController
public class DataControl {

    @Autowired
    private RealDataService realDataService;


    @Autowired
    private DataService dataService;
    /**
     * 获取站点实时数据
     * @param stationId 站点id
     * @return ListObject
     */
    @RequestMapping("/getRealData")
    public ListObject getRealDataByStationId(@RequestParam(value = "stationId")String stationId){
        return ResponseUtil.queryListObject(realDataService.getRealDataByStationId(stationId),"获取站点实时数据成功");
    }


    /**
     * 历史数据
     * @param stationId 站点id
     * @param dateType  数据类型
     * @param startTime 开始时间
     * @param endTime  结束时间
     * @param pageNum  页码
     * @return ListObject
     */
    @RequestMapping("/getHistoryData")
    public ListObject getHistoryData(@RequestParam(value = "stationId")String stationId,
                                             @RequestParam(value = "dataType")String dateType,
                                             @RequestParam(value = "startTime")String startTime,
                                             @RequestParam(value = "endTime")String endTime,
                                             @RequestParam(value = "pageNum",required = false)String pageNum) throws CloneNotSupportedException{
        try {
            return ResponseUtil.queryListObject(realDataService.queryHistoryData(stationId,startTime,endTime,dateType,pageNum),"获取历史数据成功");
        } catch (ParseException e) {
            e.printStackTrace();
            return ResponseUtil.error();
        }
    }

    /**
     * 获取对比数据
     * @param type 对比类型
     * @param time  时间
     * @param dataType  数据类型
     * @param stationId 站点id
     * @return ListObject
     */
    @RequestMapping("/getContrastData")
    public ListObject getContrastData(@RequestParam(value = "type")String type,
                                              @RequestParam(value = "time")String time,
                                              @RequestParam(value = "dataType")String dataType,
                                              @RequestParam(value = "stationId")String stationId){
        try {
            return ResponseUtil.queryListObject(dataService.getContrastData(type,time,dataType,stationId),"获取趋势分析数据成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.error();
        }
    }

    /**
     * 查询异常数据
     * @param stationId 站点id
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return ListObject
     * @throws Exception 时间格式化异常
     */
    @RequestMapping("/getAbnormalData")
    public ListObject getAbnormalData(@RequestParam(value = "stationId")String stationId,
                                      @RequestParam(value = "startTime")String startTime,
                                      @RequestParam(value = "endTime")String endTime,
                                      @RequestParam(value = "projects")String projects,
                                      @RequestParam(value = "pageNum",required = false)String pageNum,
                                      @RequestParam(value = "pageSize",required = false)String pageSize) throws Exception{
        if (pageNum!=null){
            return ResponseUtil.queryListObject(StatusCode.pagechange(dataService.getAbnormalData(stationId,startTime,endTime,projects),pageNum,pageSize),"成功");
        }else {
            return ResponseUtil.queryListObject(dataService.getAbnormalData(stationId,startTime,endTime,projects),"成功");
        }
    }

    /**
     * 获取因子
     * @param userId 用户id
     * @return ListObject
     */
    @RequestMapping("/getProjectArea")
    public ListObject getProjectArea(@RequestParam(value = "userId")String userId){
        return ResponseUtil.queryListObject(dataService.getProjectArea(userId),"成功");
    }

    /**
     * 设置因子
     * @param projectId 因子id
     * @param upper 上限
     * @param flower 下限
     * @param level_1 一级
     * @param level_2 二级
     * @param level_3 三级
     * @return ListObject
     */
    @RequestMapping("/setProjectArea")
    public ListObject setProjectArea(@RequestParam(value = "projectId")String projectId,
                                             @RequestParam(value = "upper")String upper,
                                             @RequestParam(value = "flower")String flower,
                                             @RequestParam(value = "level_1")String level_1,
                                             @RequestParam(value = "level_2")String level_2,
                                             @RequestParam(value = "level_3")String level_3){
        int num = dataService.setProjectArea(projectId,upper,flower,level_1,level_2,level_3);
        if (num==-1){
            return ResponseUtil.error("请按从大到小或从小到大的顺序设置");
        }else if(num!=0){
            return ResponseUtil.success();
        }else{
            return ResponseUtil.error();
        }
    }

    /**
     * 获取最近24小时数据
     * @param stationId 站点id
     * @return ListObject
     * @throws ParseException 时间格式化异常
     */
    @RequestMapping("/getRecentlyData")
    public ListObject getRecentlyData(@RequestParam(value = "stationId")String stationId) throws ParseException{
        return ResponseUtil.queryListObject(dataService.getRecentlyData(stationId),"成功");
    }


}
